home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / DistUpgrade / DistUpgradeMain.py < prev    next >
Text File  |  2009-11-02  |  5KB  |  136 lines

  1. # DistUpgradeMain.py 
  2. #  
  3. #  Copyright (c) 2004-2008 Canonical
  4. #  
  5. #  Author: Michael Vogt <michael.vogt@ubuntu.com>
  6. #  This program is free software; you can redistribute it and/or 
  7. #  modify it under the terms of the GNU General Public License as 
  8. #  published by the Free Software Foundation; either version 2 of the
  9. #  License, or (at your option) any later version.
  10. #  This program is distributed in the hope that it will be useful,
  11. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. #  GNU General Public License for more details.
  14. #  You should have received a copy of the GNU General Public License
  15. #  along with this program; if not, write to the Free Software
  16. #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. #  USA
  18.  
  19. import warnings
  20. warnings.filterwarnings("ignore", "Accessed deprecated", DeprecationWarning)
  21.  
  22. from DistUpgradeController import DistUpgradeController
  23. from DistUpgradeConfigParser import DistUpgradeConfig
  24. import logging
  25. import os
  26. import sys
  27. import glob
  28. import shutil
  29. import atexit
  30. from datetime import datetime
  31. from optparse import OptionParser
  32. from gettext import gettext as _
  33.  
  34.  
  35. def do_commandline():
  36.     " setup option parser and parse the commandline "
  37.     parser = OptionParser()
  38.     parser.add_option("-s", "--sandbox", dest="useAufs", default=False,
  39.                       action="store_true",
  40.                       help=_("Sandbox upgrade using aufs"))
  41.     parser.add_option("-c", "--cdrom", dest="cdromPath", default=None,
  42.                       help=_("Use the given path to search for a cdrom with upgradable packages"))
  43.     parser.add_option("--have-prerequists", dest="havePrerequists",
  44.                       action="store_true", default=False)
  45.     parser.add_option("--with-network", dest="withNetwork",action="store_true")
  46.     parser.add_option("--without-network", dest="withNetwork",action="store_false")
  47.     parser.add_option("--frontend", dest="frontend",default=None,
  48.                       help=_("Use frontend. Currently available: \n"\
  49.                              "DistUpgradeViewText, DistUpgradeViewGtk, DistUpgradeViewKDE"))
  50.     parser.add_option("--mode", dest="mode",default="desktop",
  51.                       help=_("*DEPRECATED* this option will be ignore"))
  52.     parser.add_option("--partial", dest="partial", default=False,
  53.                       action="store_true", 
  54.                       help=_("Perform a partial upgrade only (no sources.list rewriting)"))
  55.     parser.add_option("--datadir", dest="datadir", default=None,
  56.                       help=_("Set datadir"))
  57.     return parser.parse_args()
  58.     
  59. def setup_logging(options, config):
  60.     " setup the logging "
  61.     logdir = config.getWithDefault("Files","LogDir","/var/log/dist-upgrade/")
  62.     if not os.path.exists(logdir):
  63.         os.mkdir(logdir)
  64.     # check if logs exists and move logs into place
  65.     if glob.glob(logdir+"/*.log"):
  66.         now = datetime.now()
  67.         backup_dir = logdir+"/%04i%02i%02i-%02i%02i" % (now.year,now.month,now.day,now.hour,now.minute)
  68.         if not os.path.exists(backup_dir):
  69.             os.mkdir(backup_dir)
  70.         for f in glob.glob(logdir+"/*.log"):
  71.             shutil.move(f, os.path.join(backup_dir,os.path.basename(f)))
  72.     fname = os.path.join(logdir,"main.log")
  73.     # do not overwrite the default main.log
  74.     if options.partial:
  75.         fname += ".partial"
  76.     logging.basicConfig(level=logging.DEBUG,
  77.                         filename=fname,
  78.                         format='%(asctime)s %(levelname)s %(message)s',
  79.                         filemode='w')
  80.     # log what config files are in use here to detect user
  81.     # changes
  82.     logging.info("Using config files '%s'" % config.config_files)
  83.     return logdir
  84.     
  85. def setup_view(options, config, logdir):
  86.     " setup view based on the config and commandline "
  87.  
  88.     # the commandline overwrites the configfile
  89.     for requested_view in [options.frontend]+config.getlist("View","View"):
  90.         if not requested_view:
  91.             continue
  92.         try:
  93.             view_modul = __import__(requested_view)
  94.             view_class = getattr(view_modul, requested_view)
  95.             instance = view_class(logdir=logdir)
  96.             break
  97.         except Exception, e:
  98.             logging.warning("can't import view '%s' (%s)" % (requested_view,e))
  99.             print "can't load %s (%s)" % (requested_view, e)
  100.     else:
  101.         logging.error("No view can be imported, aborting")
  102.         print "No view can be imported, aborting"
  103.         sys.exit(1)
  104.     return instance
  105.  
  106. def main():
  107.     " main method "
  108.     
  109.     # commandline setup and config
  110.     (options, args) = do_commandline()
  111.     config = DistUpgradeConfig(".")
  112.     logdir = setup_logging(options, config)
  113.  
  114.     from DistUpgradeVersion import VERSION
  115.     logging.info("release-upgrader version '%s' started" % VERSION)
  116.  
  117.     # create view and app objects
  118.     view = setup_view(options, config, logdir)
  119.     app = DistUpgradeController(view, options, datadir=options.datadir)
  120.     atexit.register(app._enableAptCronJob)
  121.  
  122.     # partial upgrade only
  123.     if options.partial:
  124.         if not app.doPartialUpgrade():
  125.             sys.exit(1)
  126.         sys.exit(0)
  127.  
  128.     # full upgrade, return error code for success/failure
  129.     if app.run():
  130.         return 0
  131.     return 1
  132.  
  133.